package ltd.hyangyang.vegetable.utils;

import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ScriptUtils {

    @SneakyThrows
    public static String runScript(String[] args){
//        String[] args = {"python","../script/modelload.py",}
        Process process = Runtime.getRuntime().exec(args);
        InputStream inputStream = process.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String out ="";
        String line = null;
        while ((line=bufferedReader.readLine())!=null){
            if (out.length()!=0){
               out=out+"\n";
            }
            out=out+line;

        }
//        String line = bufferedReader.readLine();
        bufferedReader.close();
        InputStream errorStream = process.getErrorStream();
        InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
        BufferedReader errorBufferReader = new BufferedReader(errorStreamReader);
        String errorLine = null;
        while ((errorLine=errorBufferReader.readLine())!=null){
            System.out.println(errorLine);
        }
        System.out.println(process.waitFor());
        System.out.println(out);
        return out;
    }
}
